package org.inasnet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

public class Config extends StringMap
{
	public Config( String configFile )
	{
		loadAsConfigFormatFromFile( configFile );
		
		mConfigFile = configFile;
	}
	
	void setParameter( String key, String type )
	{
		set( key, type );
		
		updateConfig();
	}
	
	private void updateConfig() 
	{
		// delete file
		File file = new File( mConfigFile );
		file.delete();
		
		// create new
		file = new File( mConfigFile );
		
		try 
		{
			file.createNewFile();
		
			FileOutputStream fileStream = new FileOutputStream( file );
			
			Set< Entry< String, String > > set = getValues();
			
			Iterator< Entry< String, String > > iter = set.iterator();
			
			Entry< String, String > pair = iter.next();
			
			while( iter.hasNext() )
			{
				String write = pair.getKey() + " " + pair.getValue();
				
				fileStream.write( write.getBytes() );
				
				pair = iter.next();
			}
			
			fileStream.close();
		
		} 
		catch( Exception e ) 
		{
			e.printStackTrace();
		}
	}

	String mConfigFile;
}
